<?php

//#########################################################################
//
//     com_myrss_rssfilter
//
// (C) Copyright by Hachreak
//       17/dic/07 - 23:52:15
//       Released under GPL v2.
//       Author : Hachreak (hachreak at gmail dot com)
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
//#########################################################################


defined('_VALID_MOS') or die('Access Denied');

// libreria per estrarre testo da codice html
require_once($mosConfig_absolute_path.'/components/com_myrss_rssfilter/html2text/html2text.php');
// libreria per manipolare l'rss letto
require_once($mosConfig_absolute_path.'/components/com_myrss_rssfilter/inc.rss2array.php');
// libreria di utilita'
require_once($mosConfig_absolute_path.'/components/com_myrss_rssfilter/dec.html.php');

// ultima versione
$LAST_VERSION = "0.7.1";
// link dove trovare l'ultima versione
$LAST_VERSION_WWW = "http://www.secondlifelab.it/index.php?option=com_remository&Itemid=77&func=select&id=5";
// Lunghezza max del messaggio di risposta
$MAX_LENGTH_MSG = 510;
// Url dell'rss di default
$DEFAULT_URL = "http://www.secondlifelab.it/index.php?option=com_rss&feed=RSS2.0&no_html=1";
// tabella dove sono salvate le statistiche
$MYRSS_TABLE = "#__myrss_rssfilter";

// Get the operation to do
$op = mosGetParam($_GET,"op","");

// decode the url
$url_dec = decode_url($url);

function adobeNews($title, $body, $link){
	 $out  = " -\t-\t-\t-\t\n";
	 $out .= strtoupper($title)."\n";
     $out .= " -\t-\t-\t-\t\n\n";
     
	 $out .= $body;
	 
	 $puntini = "(...)";
     $read_more = "\n\nRead more: ".$news["link"];

     $length_str = strlen($out);
     $length_tail = strlen($puntini) + strlen($read_more);

     # elimina il superfluo
     $out = substr($out, 0, $MAX_LENGTH_MSG - $length_tail);

     # se il messaggio e' stato tagliato, aggiunti i puntini di sospensione
     if($length_str > strlen($out)){
            $out .= $puntini;
     }

     # Aggiungi il "read more"
     $out .= $read_more;
	 
     return $out;
}

/**
 * Statistiche
 *
 */
class myrss_stat{
	var $url = "";
	var $titles = 0;
	var $news = 0;
	var $last_version = 0;
	var $version = "";
	var $region = "";
	var $coo_x = 0;
	var $coo_y = 0;
	var $coo_z = 0;
}

// Oggetto per le statistiche
$stat = new myrss_stat();

switch($op){
        
        case 'version':
        	// Ottengo la versione del client
        	$version = mosGetParam($_GET,"version","");
        	
        	$out = "MYrss last version: ".$LAST_VERSION."\n";
        	
			if($version != $LAST_VERSION){
                // Avverti che e' presente una versione piu' recente
                $out .= "\n Your version is old!\n";
                $out .= "Download last version: ".$LAST_VERSION_WWW."\n";
        	}
        	
        	echo $out;
        	
        	// TODO salva le statistiche
			$stat->last_version = 1;
        	break;
        	 
        default:

			// Get the url of RSS
			$url = mosGetParam($_GET,"url",$DEFAULT_URL);
			
			// TODO salvo le statistiche
			$stat->url = $url; 
			
			// Leggi l'rss
			$rss_array = rss2array(decode_url($url));
			
			// Controllo se ci sono stati errori
			if(count($rss_array[errors]) > 0){
                header ("HTTP/1.0 501 Not Found");
                exit();
        	}
        	
        	switch($op){
        		case 'body':
        			// Get the number of news to read
					$number = mosGetParam($_GET,"number",0);
					
					 // read the news number $number
        			$news = $rss_array["items"][$number];
        			
        			$ht = new html2text(false, 0, false);
        			
        			echo adobeNews(
        					convert_enc_chars($news["title"]), 
        					convert_enc_chars(
        						preg_replace(
        							"|<[^>]+>|U","",
        							$ht->load_string($news["description"])
        						)
        					), 
        					$news["link"]
        			);
        			
        			// TODO salva le statistiche
					$stat->news = 1;
        			        			
        			break;
        			
        		default:
        			// Read the news titles
        			for($i=0;$i<count($rss_array["items"]);$i++){
                		$news = $rss_array["items"][$i];
                		echo convert_enc_chars($news["title"])."\n";
        			}
        			
        			// TODO salva le statistiche
					$stat->last_version = 1;
        			
        			break;
        	}
			
        	break;
}

// TODO salva le statistiche
$stat->region = mosGetParam($_GET,"region","");
$stat->coo_x = mosGetParam($_GET,"coo_x",0);
$stat->coo_y = mosGetParam($_GET,"coo_y",0);
$stat->coo_z = mosGetParam($_GET,"coo_z",0);
$stat->version = mosGetParam($_GET,"version","");

// salvo le statistiche nel database
global $database;
$database->insertObject($MYRSS_TABLE, $stat);

?>